<?php
namespace jy_merch;
defined('BY_JYA') or exit('error');
/*
	本插件：$this->model('order')->方法名();
	其他地方调用：model('jy_merch/order')->方法名();
*/
class order extends \plugin{
	public function buy_credit3($order=array(),$iscode=false){
		$buy_credit=plugin('jy_weishop')->plugin_setting('buy_credit3');
		$merchset=$this->plugin_setting('buy_credit3');
		//交由商户处理
		if(!$merchset['is_codepay'] && $iscode){
			return 0;
		}
		if($merchset['open']){
			$merchset['ismerch']=1;
			$buy_credit=$merchset;
		}
		if($order['paytype']=='wechat' && strpos($buy_credit['paytypes'],';1;') === false){
			return 0;
		}
		if($order['paytype']=='credit' && strpos($buy_credit['paytypes'],';2;') === false){
			return 0;
		}
		if($order['paytype']=='alipay' && strpos($buy_credit['paytypes'],';3;') === false){
			return 0;
		}
		//根据订单金额赠送积分
		if(!$buy_credit['open']){
			return 0;
		}
		$credit=array();
		if($buy_credit['options']){
			foreach($buy_credit['options'] as $item){
				if($order['price']>=$item['start'] && $order['price']<=$item['end']){
					$credit[$item['type']]=intval($order['price']*$item['credit']);
				}
			}
		}
		if($order['merchid']){
			//是否扣部分币给商户
			$merchset=$this->plugin_setting('setting');
			if($merchset['is_koucredit']){
					$merchinfo=pdo_get('jy_merch_user',array('id'=>$order['merchid'],'uniacid'=>$_SESSION['uniacid']),array('choufee_set','mid'));
					$choufee_set=json_decode($merchinfo['choufee_set'],true);
					$koucredit_rate=$choufee_set['koucredit_rate']?:$merchset['koucredit_rate'];
					$merchmid=$merchinfo['mid'];
			}
		}
		foreach($credit as $type=>$num){
			if($num<=0){
				continue;
			}
			if($koucredit_rate && $merchmid){
				$kounum=$num*$koucredit_rate/100;
				model('core/member')->member_credit($merchmid,'credit'.$type,$kounum,true,'jy_merch','客户付款商户获币','buy_credit3');
				$num -=$kounum;
			}
			model('core/member')->member_credit($order['mid'],'credit'.$type,$num,true,'jy_merch','客户付款送币','buy_credit3');
		}
		return $credit;
	}
}